clear
insheet using "nhgis0019_ds78_1940_county.csv"

gen cty_fips = statea*100 + (countya/10)

*Variables for Table A35
gen totpop1940 = bv7001

gen urbpop1940 = bw1001
gen fracurb1940 = urbpop1940/totpop1940

gen popcitiesover25k1940 = bxd001
gen fraccities1940 = popcitiesover25k1940/totpop1940

gen male1940 = bxn001
gen fracmale1940 = male1940/totpop1940

gen black1940 = bya003
gen fracblack1940 = black1940/totpop1940

gen foreign1940 = bya002
gen fracforeign1940 = foreign1940/totpop1940

gen mfgestab = bwz001
gen lnmfgestab = ln(mfgestab)

gen empmfg = bw0001

gen wagesmfg = bw2001
gen avgwagemfg = wagesmfg/empmfg
gen lnavgwagemfg = ln(avgwagemfg)

gen valueaddedmfg = bw5001
gen lnvalueaddedmfg = ln(valueaddedmfg)

egen pop14andover = rowtotal(bw7001 bw7002), missing
egen emp14andover = rowtotal(bw9001 bw9002), missing
egen seekwork14andover = rowtotal(bxc001 bxc002), missing

local vars emp14andover seekwork14andover
foreach x of local vars{
gen frac`x' = `x'/pop14andover
}

gen fracownerocc = bxw001/100
gen fracelectric = byb001/100
gen fracradio = byg001/100
gen fracfridge = byi001/100
gen avgvalueownerocc = byo001
gen medvalueownerocc = byp001
gen valuecrops = byx002

local vars avgvalueownerocc medvalueownerocc valuecrops
foreach x of local vars{
gen ln`x' = ln(`x')
}

****Variables for Table A37
gen attend56 = bwu001
gen attend713 = bwu002
gen attend1415 = bwu003
gen attend1617 = bwu004
gen attend1820 = bwu005
gen attend2124 = bwu006

gen pop56 = bwt001
gen pop713 = bwt002
gen pop1415 = bwt003
gen pop1617 = bwt004
gen pop1820 = bwt005
gen pop2124 = bwt006

gen fracattend56 = attend56/pop56
gen fracattend713 = attend713/pop713
gen fracattend1415 = attend1415/pop1415
gen fracattend1617 =  attend1617/pop1617
gen fracattend1820 = attend1820/pop1820
gen fracattend2124 = attend2124/pop2124

gen medschoolcompl25overmale = byt001
gen medschoolcompl25overfemale = byt002

egen pop25andover = rowtotal(bwv001 bwv002), missing
egen pop25andovercollege = rowtotal(bww008 bww017), missing
egen pop25andoversomecoll = rowtotal(bww005 bww014), missing
egen pop25andoverhs = rowtotal(bww006 bww015), missing 

local vars 25andovercollege 25andoversomecoll 25andoverhs
foreach x of local vars{
gen frac`x' = pop`x'/pop25andover
}

*Variables for Table A36
#delimit ;
egen emp = rowtotal(bxf001 bxf002 bxf003 bxf004 bxf005 bxf006 bxf007 bxf008 bxf009 
bxf010 bxf011 bxf012 bxf013 bxf014 bxf015 bxf016 bxf017 bxf018 bxf019 bxf020 bxf021 
bxf022 bxf023 bxf024 bxf025 bxf026), missing;

#delimit cr
egen prof = rowtotal(bxf001 bxf002), missing
egen semiprof = rowtotal(bxf003 bxf004), missing
egen farmer = rowtotal(bxf005 bxf006), missing
egen mgr = rowtotal(bxf007 bxf008), missing
egen clerk = rowtotal(bxf009 bxf010), missing
egen crafts = rowtotal(bxf011 bxf012), missing
egen oper = rowtotal(bxf013 bxf014), missing
egen domestic = rowtotal(bxf015 bxf016), missing
egen srv = rowtotal(bxf017 bxf018), missing
egen laborer = rowtotal(bxf023 bxf024), missing

#delimit ;
local vars prof semiprof farmer mgr clerk crafts oper domestic srv laborer;

foreach x of local vars{;

gen frac`x' = `x'/emp;
};

#delimit cr

**********
*Regressions and Analysis for Table A35
merge 1:1 cty_fips using "justnormasylum.dta"
rename _merge merge1940

replace hasnormalschool = 0 if merge1940==1

reghdfe totpop1940 hasnormalschool if hasnormalorasylum==1, absorb(state)
gen samp = e(sample)==1

#delimit ;
pca totpop1940 fracurb1940 fraccities1940 fracmale1940 fracblack1940 
fracforeign1940  lnavgwagemfg lnvalueaddedmfg  lnmfgestab
fracemp14andover fracseekwork14andover fracownerocc 
fracelectric fracradio fracfridge lnavgvalueownerocc lnmedvalueownerocc 
lnvaluecrops if samp==1;

predict pc1 pc2 pc3 pc4 pc5 pc6 pc7 pc8 pc9 pc10 pc11 pc12 pc13 pc14 pc15 pc16 pc17 pc18, score;


#delimit ;
local vars totpop1940 fracurb1940 fraccities1940 fracmale1940 fracblack1940 
fracforeign1940 lnavgwagemfg lnvalueaddedmfg lnmfgestab
fracemp14andover fracseekwork14andover fracprof 
fracsemiprof fracfarmer fracmgr fracclerk fraccrafts fracoper fracdomestic 
fracsrv  fraclaborer fracownerocc 
fracelectric fracradio fracfridge lnavgvalueownerocc lnmedvalueownerocc 
lnvaluecrops  
fracattend56 fracattend713 fracattend1415 fracattend1617 fracattend1820 fracattend2124 
medschoolcompl25overmale medschoolcompl25overfemale 
frac25andovercollege frac25andoversomecoll frac25andoverhs pc1;

#delimit cr
foreach x of local vars{
reghdfe `x' hasnormalschool if hasnormalorasylum==1, absorb(state) cluster(state)
outreg2 using char_1940.xls, append dec(3) excel

#delimit ;
tabout hasnormalschool if hasnormalorasylum==1 using sumstats_1940.xls,
append c(mean `x' sd `x') npos(row) f(2)  layout(row) sum show(none); 

#delimit cr
reghdfe `x' hasnormalschool, absorb(state)

su `x' if hasnormalorasylum==1 & hasnormalschool==1
su `x' if hasnormalorasylum==1 & hasnormalschool==0
}

save "nhgis_1940.dta", replace

keep stateicp countyicp cty_fips hasnormalschool hasnormalorasylum
save "icpfips.dta", replace
